package cn.tedu._03vehicle_test.controller;

import cn.tedu._03vehicle_test.base.response.JsonResult;
import cn.tedu._03vehicle_test.base.response.StatusCode;
import cn.tedu._03vehicle_test.mapper.VehicleMapper;
import cn.tedu._03vehicle_test.pojo.dto.UserRegParam;
import cn.tedu._03vehicle_test.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle_test.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle_test.pojo.entity.Vehicle;
import cn.tedu._03vehicle_test.pojo.vo.UserLoginVO;
import cn.tedu._03vehicle_test.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Property;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "01.车辆管理功能")
@Slf4j
@RestController
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @ApiOperation("添加车辆功能")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session){
        log.debug("开始添加车辆！");
        UserLoginVO userVO = (UserLoginVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        int num = vehicleMapper.addVehicle(vehicle);
        if (num>0){
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @GetMapping("list")
    @ApiOperation("车辆列表")
    @ApiOperationSupport(order = 20)
    public JsonResult vehicleList(VehicleListQuery vehicleListQuery,@ApiIgnore HttpSession session){
       log.debug("开始查询列表：");
       UserLoginVO userVO = (UserLoginVO) session.getAttribute("user");
       if(userVO == null){
           return new JsonResult(StatusCode.NOT_LOGIN);
       }
       List<VehicleListVO> vehicleList = vehicleMapper.Listselect(vehicleListQuery);
       return JsonResult.ok(vehicleList);
    }
    @PostMapping("del/{id}")
    @ApiOperation("删除车辆信息")
    @ApiOperationSupport(order = 40)
    public JsonResult delVehicle(@PathVariable Long id, @ApiIgnore HttpSession session){
        log.debug("开始删除车辆信息：");
        UserLoginVO userVO = (UserLoginVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Long userId = userVO.getId();
        int i = vehicleMapper.deleteVehicle(id,userId);
        if(i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.NOT_IllEGAL_USER);
    }
}
